from xml.etree.ElementTree import ElementTree, dump, fromstring



class RecipeXml(object):
	def __init__(self, xmlstr = ""):
		self.name = ''
		self.target = []

		self.__loadfromstring(xmlstr)


	def __load(self, element = None):
		if element != None:
#			try:
			recipe = element.find("recipe")
			if recipe != None:
				self.name = recipe.attrib['name']
				targets = recipe.findall("target")
				for t in targets:				
					self.target.append(RecipeTargets(t))

#			except:
#				pass

		else:
			return None

	def __loadfromstring(self, xmlstr = ''):
		if xmlstr != '':
#			try:
			self.__load(fromstring(xmlstr))
#			except:
#				pass


						

class RecipeDst(object):
	def __init__(self, element = None):
		self.host   = ''
		self.user   = ''
		self.passwd = ''
		self.path   = ''

		self.__load(element)

	def __load(self, element = None):
		if element != None:
#			try:
			self.host   = element.find('host').text
			self.user   = element.find('user').text
			self.passwd = element.find('passwd').text
			self.path   = element.find('path').text 

#			except:
#				pass

class RecipeCarbon(object):
	def __init__(self, element = None):
		self.ext    = ''
		self.guid   = ''
		self.__load(element)

	def __load(self, element = None):
		if element != None:
#			try:
			self.guid = element.find('guid').text
			self.ext  = element.find('ext').text

#			except:
#				pass

class RecipeTargets(object):
	def __init__(self, element = None):
		self.name            = ''
		self.transcode       = False
		self.transcodemethod = ''
		self.destination    = []
		self.transcoder      = None
		
		self.__load(element)
	
	def __load(self, element = None):
		if element != None:
#			try:
			self.name = element.attrib['name']
			if element.attrib['transcode'] == 'True':
				self.transcode = True
				self.transcodemethod = element.attrib['transcodemethod']
				if self.transcodemethod == 'carbon':
					self.transcoder = RecipeCarbon(element.find(self.transcodemethod))
			else:
				self.transcode = False

			dest = element.findall('destination')
			for d in dest:
				self.destination.append(RecipeDst(d))				

#			except:
#				pass



